import processing.core.PApplet;
import processing.serial.*;

	// If this isn't working for you, make sure that the port name is correct
	private static final String PORT_NAME = "COM7";

	private class Button {
		private static final int BASE_COLOR = 40;
		private static final int HIGHLIGHT_COLOR = 80;
		private static final int SELECTED_COLOR = 120;

		public static final int WIDTH = 100;
		public static final int HEIGHT = 30;

		int x, y;
		boolean selected = false;

		public Button(int x, int y) {
			this.x = x;
			this.y = y;
		}

		private boolean isMouseOver() {
			return mouseX >= x && mouseX <= x + WIDTH && mouseY >= y
					&& mouseY <= y + HEIGHT;
		}

		public void display() {
			stroke(255);
			int currentColor;
			if (selected || isMouseOver() && mousePressed) {
				currentColor = SELECTED_COLOR;
				onMouseClick(this);
				selected = true;
			} else {
				currentColor = isMouseOver() ? HIGHLIGHT_COLOR : BASE_COLOR;
			}

			fill(currentColor);
			rect(x, y, WIDTH, HEIGHT);
		}

		public void setUnselected() {
			selected = false;
		}

		public boolean isSelected() {
			return selected;
		}
	}
        
        // if you're using the displayduino, this needs to be half of the arg to Serial.begin() on the displayduino
	private static final int BAUD_RATE = 9600;

	public void setup() {

		serialPort = new Serial(this, PORT_NAME, BAUD_RATE);

		textFont(loadFont("Candara-48.vlw"), 20);

		size(550, 125);
		background(200);

		for (int i = 0; i < 3; i++) {
			buttons[i] = new Button(BUTTON_X
					+ (Button.WIDTH + BUTTON_WIDTH_PADDING) * i, BUTTON_Y);
		}
		for (int i = 3; i < 6; i++) {
			buttons[i] = new Button(BUTTON_X
					+ (Button.WIDTH + BUTTON_WIDTH_PADDING) * (i - 3),
					(BUTTON_Y * 2) + 5);
		}
	}

	private void onMouseClick(Button clicked) {
		for (Button button : buttons) {
			if (!button.equals(clicked)) {
				button.setUnselected();
			}
		}
	}

	private int selectedButtonIndex() {
		for (int i = 0; i < buttons.length; i++) {
			if (buttons[i].isSelected()) {
				return i;
			}
		}

		return -1;
	}

	private Serial serialPort;

	private static final int BUTTON_WIDTH_PADDING = 25;
	private static final int BUTTON_X = 150;
	private static final int BUTTON_Y = 35;

	// I sure hope this stays in sync between this file and Controller.pde!
	private static final int LOWER_GOAL_THRESHOLD = 400;
	private static final int UPPER_GOAL_THRESHOLD = 600;

	private Button[] buttons = new Button[6];

        private int lastSentVal;
	public void draw() {
		for (Button button : buttons) {
			button.display();
		}

		fill(0, 102, 153);
		int top = 60;
		int x = 10;
		text("Increasing", x, top);
		text("Decreasing", x, top + 40);

		int left = 150;
		int y = 20;
		text("Below goal", left, y);
		text("At goal", left + 150, y);
		text("Above goal", left + 250, y);

		int selectedIndex = selectedButtonIndex();
                if (selectedIndex != -1) {
                        int maxVal, minVal;
        		if (selectedIndex == 0 || selectedIndex == 3) { // below goal
        		  maxVal = LOWER_GOAL_THRESHOLD - 10;
                          minVal = 0;

        		} else if (selectedIndex == 1 || selectedIndex == 4) { // at goal
                          maxVal = UPPER_GOAL_THRESHOLD - 10;
                          minVal = LOWER_GOAL_THRESHOLD + 10;
                          
                        } else {  // above goal
                          maxVal = UPPER_GOAL_THRESHOLD + 200;
                          minVal = UPPER_GOAL_THRESHOLD + 10;
                          
                        }
                        
                        int delta = 5;
                        // decreasing
                        if (selectedIndex > 2) {
                          delta *= -1;
                        }
                        
                        lastSentVal += delta;
                        
                        if (lastSentVal > maxVal) {
                          lastSentVal = minVal;
                        }
                        if (lastSentVal < minVal) {
                          lastSentVal = maxVal;
                        }
                        
                        //println(lastSentVal); 
                        
                        serialPort.write(lastSentVal + ".");

                }
                
                while (serialPort.available() > 0) {
                  print((char)serialPort.read());
                }
	}


